package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.exception.ExceptionCast;
import com.atguigu.common.exception.ExceptionCodeEnum;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.AttrEntityVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(AttrEntityVo attrEntityVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrEntityVo, attrEntity);
        baseMapper.insert(attrEntity);
        //保存 属性与属性分组的关系
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
        attrAttrgroupRelationEntity.setAttrGroupId(attrEntityVo.getAttrGroupId());
        attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
        attrAttrgroupRelationService.save(attrAttrgroupRelationEntity);
    }

    @Override
    public PageUtils getDetailList(Map<String, Object> params, Long catId) {

        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>();

        if (!(catId == null || catId == 0)) {
            wrapper.eq("catelog_id", catId);
        }

        String key = (String) params.get("key");

        if (!StringUtils.isEmpty(key)) {
            //wrapper.like("attr_name", key).or().eq("attr_id", key);
            wrapper.and((queryWrapper) -> {
                queryWrapper.like("attr_name", key).or().eq("attr_id", key);
            });
        }

        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);

        PageUtils pageUtils = new PageUtils(page);


        List<AttrEntity> attrEntities = page.getRecords();
        List<AttrEntityVo> attrEntityVoList = new ArrayList<AttrEntityVo>();

        for (AttrEntity entity : attrEntities) {
            AttrEntityVo attrEntityVo = new AttrEntityVo();
            BeanUtils.copyProperties(entity,attrEntityVo);

            //设置分类名和分组名
            CategoryEntity categoryEntity = categoryService.findOneById(entity.getCatelogId());

            if (categoryEntity == null) {
                ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
            }

            attrEntityVo.setCatelogName(categoryEntity.getName());

            AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", entity.getAttrId()));

            AttrGroupEntity groupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());

            if (groupEntity == null) {
                ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
            }

            attrEntityVo.setGroupName(groupEntity.getAttrGroupName());

            attrEntityVoList.add(attrEntityVo);
        }

        pageUtils.setList(attrEntityVoList);

        return pageUtils;
    }

    @Override
    public AttrEntityVo getDetailById(Long attrId) {

        AttrEntity attrEntity = baseMapper.selectById(attrId);

        if (attrEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        AttrEntityVo attrEntityVo = new AttrEntityVo();

        BeanUtils.copyProperties(attrEntity,attrEntityVo);

        AttrAttrgroupRelationEntity relationEntity = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));

        AttrGroupEntity groupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());

        if (groupEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        attrEntityVo.setGroupName(groupEntity.getAttrGroupName());
        attrEntityVo.setAttrGroupId(groupEntity.getAttrGroupId());

        CategoryEntity categoryEntity = categoryService.findOneById(attrEntity.getCatelogId());

        if (categoryEntity == null) {
            ExceptionCast.exception(ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getMsg(), ExceptionCodeEnum.DATA_NOT_EXIST_EXCEPTION.getCode());
        }

        attrEntityVo.setCatelogName(categoryEntity.getName());
        attrEntityVo.setCatelogId(categoryEntity.getCatId());
        attrEntityVo.setCatelogPath(categoryService.getCategoryPathById(categoryEntity.getCatId()));

        return attrEntityVo;
    }

}